package com.zhm.demo.email;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.File;
import java.util.Properties;

public class JavaMailSender {
    public static void main(String[] args) throws Exception {
//        String host = "smtp.163.com";
//        int port = 465;
//        String username = "zhanghuamingmyself@163.com";
//        String password = "GXASEGJUYRJOKPRY";


        String host = "mail.hithium.cn";
        int port = 587;
        String username = "HeroEE";
        String password = "Hc@wsxcde#321";


        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);

        mailSender.setUsername(username);
        mailSender.setPassword(password);

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
//        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.debug", "true");

        String to = "zhanghuamingmyself@163.com";
        String subject = "测试 subject";
        String text = "测试 text";

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailSender.getUsername());
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);

//        String pathToAttachment="";
//
//        MimeMessage mimeMessage = mailSender.createMimeMessage();
//
//        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//
//        helper.setFrom("noreply@baeldung.com");
//        helper.setTo(to);
//        helper.setSubject(subject);
//        helper.setText(text);
//
//        FileSystemResource file
//                = new FileSystemResource(new File(pathToAttachment));
//        helper.addAttachment("Invoice", file);
//
//        mailSender.send(message);
    }
}
